package com.example.administrator.test;

import android.annotation.SuppressLint;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

public class BookDetailActivity extends AppCompatActivity {

    //评论部分
    ListView listView;
    List<Comment> list_comment = new ArrayList<>();
    CommentAdapter commentAdapter;
    EditText content;
    Button submit;

    Book book;
    List<Collect> collect_count = new ArrayList<>();
    int counts = 0;

    private MyApplication app1;
    private MyApplication app2;
    DbManager db;

    private ImageView img;
    private TextView textView;
    private ImageView collect;
    private TextView count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }
        initView();
        //Toast.makeText(BookDetailActivity.this, app2.getAccount(), Toast.LENGTH_SHORT).show();
        getComment();
        commentAdapter = new CommentAdapter(this,list_comment);
        listView = (ListView)findViewById(R.id.book_detail_listView);
        listView.setAdapter(commentAdapter);

    }

    private void initView() {
        book = (Book)getIntent().getSerializableExtra("book");
        img = (ImageView)findViewById(R.id.book_detail_image);
        textView = (TextView)findViewById(R.id.book_detail_title);
        collect = (ImageView)findViewById(R.id.book_detail_collect);
        img.setImageResource(getResources().getIdentifier(book.getImg(),"mipmap",getPackageName()));
        textView.setText(book.getTitle());
        count = (TextView)findViewById(R.id.book_detail_collect_count);//收藏数量

        //评论部分
        content = (EditText)findViewById(R.id.book_detail_comment_edit);
        submit = (Button)findViewById(R.id.book_detail_comment_button);

        app1 = new MyApplication();
        db = x.getDb(app1.getDaoConfig());
        app2 = (MyApplication)getApplication();

        try {
            Collect find_collect = db.selector(Collect.class).where("account","==",app2.getAccount()).and("book_id","==",book.getId()).findFirst();
            if(find_collect==null){
                collect.setImageResource(R.mipmap.tab32);
            }else{
                collect.setImageResource(R.mipmap.tab31);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        try {
            Collect test = db.selector(Collect.class).findFirst();
            if(test==null){
                count.setText("0人收藏");
            }
            else{
                collect_count = db.selector(Collect.class).where("book_id","==",book.getId()).findAll();
                if(collect_count.size()!=0){
                    counts = collect_count.size();
                    count.setText(counts+"人收藏");
                }else{
                    count.setText("0人收藏");
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        //点击收藏
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Collect find_collect = db.selector(Collect.class).where("account","==",app2.getAccount()).and("book_id","==",book.getId()).findFirst();
                    if(find_collect==null){
                        Collect temp = new Collect();
                        temp.setAccount(app2.getAccount());
                        temp.setBook_id(book.getId());
                        db.save(temp);
                        counts++;
                        count.setText(counts+"人收藏");
                        collect.setImageResource(R.mipmap.tab31);
                    }else{
                        db.delete(find_collect);
                        collect.setImageResource(R.mipmap.tab32);
                        counts--;
                        count.setText(counts+"人收藏");
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });

        //点击收藏
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(content.getText().toString().equals("")){
                    Toast.makeText(BookDetailActivity.this, "评论内容不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    Comment comment = new Comment();
                    comment.setBook_id(book.getId());
                    comment.setAccount(app2.getAccount());
                    comment.setContent(content.getText().toString());
                    comment.setTime(getTime());
                    try {
                        db.save(comment);
                        list_comment.add(0,comment);
                        commentAdapter.notifyDataSetChanged();
                        content.setText("");
                        Toast.makeText(BookDetailActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    private void getComment()
    {
        try {
            List<Comment> comments = db.selector(Comment.class).where("book_id","==",book.getId()).orderBy("id",true).findAll();
            if(comments!=null&&comments.size()!=0){
                for (Comment comment:comments){
                    list_comment.add(comment);
                }
            }else{
                Toast.makeText(BookDetailActivity.this, "无评论", Toast.LENGTH_SHORT).show();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("WrongConstant")
    public  String getTime()
    {
        //创建日历对象
        Calendar calendar = Calendar.getInstance();
        //设置时区
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = String.valueOf(calendar.get(Calendar.MONTH)+1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
        String minutes = String.valueOf(calendar.get(Calendar.MINUTE));
        String second = String.valueOf(calendar.get(Calendar.SECOND));
        String week = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if("1".equals(week)){
            week="星期天";
        }else if("2".equals(week)){
            week = "星期一";
        }else if("3".equals(week)){
            week = "星期二";
        }else if("4".equals(week)){
            week = "星期三";
        }else if("5".equals(week)){
            week = "星期四";
        }else if("6".equals(week)){
            week = "星期五";
        }else if("7".equals(week)){
            week = "星期六";
        }
        return year+"年"+month+"月"+day+"日"+" "+hour+":"+minutes+":"+second+":"+week;
    }
}
